package com.ruoyi.web.controller.system.fuw;

import java.util.*;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.fuw.domain.FuwTasks;
import com.ruoyi.fuw.service.IFuwTasksService;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fuw.domain.FuwTask;
import com.ruoyi.fuw.service.IFuwTaskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 任务分配Controller
 * 
 * @author ruoyi
 * @date 2021-11-03
 */
@RestController
@RequestMapping("/fuw/task")
public class FuwTaskController extends BaseController
{
    @Autowired
    private IFuwTaskService fuwTaskService;

    @Autowired
    private IFuwTasksService fuwTasksService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysPostService postService;

    /**
     * 查询任务分配列表
     */
    @PreAuthorize("@ss.hasPermi('fuw:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(FuwTask fuwTask)
    {
        startPage();
        List<FuwTask> list = fuwTaskService.selectFuwTaskList(fuwTask);
        return getDataTable(list);
    }

    /**
     * 查询医疗工作者medical/sysUser信息列表
     */
    @PreAuthorize("@ss.hasPermi('fuw:task:list')")
    @GetMapping("/list/medical")
    public AjaxResult listMedical()
    {
        // 查询全部医疗工作者信息
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(113L);
        sysUser.setStatus("0");
        List<SysUser> userList = userService.selectList(sysUser);

        // 过滤掉正在服务状态的医疗工作者
        List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
        for(SysUser sysUserNow:userList){
            Map<String, Object> mapNew = new HashMap<>();
            mapNew.put("value", sysUserNow.getUserName());
            mapNew.put("label", sysUserNow.getNickName());
            listMap.add(mapNew);
        }

        return AjaxResult.success(listMap);
    }

    /**
     * 查询组别岗位
     */
    @PreAuthorize("@ss.hasPermi('fuw:task:list')")
    @GetMapping("/list/post")
    public AjaxResult listPost()
    {
        // 查询全部组别
        SysPost sysPost = new SysPost();
        sysPost.setStatus("0");
        List<SysPost> postList = postService.selectPostList(sysPost);

        // 过滤掉正在服务状态的岗位组别
        List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
        for(SysPost postNew:postList){
            Map<String, Object> mapNew = new HashMap<>();
            mapNew.put("value", postNew.getPostId());
            mapNew.put("label", postNew.getPostName());
            listMap.add(mapNew);
        }

        return AjaxResult.success(listMap);
    }

    /**
     * 导出任务分配列表
     */
    @PreAuthorize("@ss.hasPermi('fuw:task:export')")
    @Log(title = "任务分配", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FuwTask fuwTask)
    {
        List<FuwTask> list = fuwTaskService.selectFuwTaskList(fuwTask);
        ExcelUtil<FuwTask> util = new ExcelUtil<FuwTask>(FuwTask.class);
        return util.exportExcel(list, "任务分配数据");
    }

    /**
     * 获取任务分配详细信息
     */
    @PreAuthorize("@ss.hasPermi('fuw:task:query')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") String taskId)
    {
        return AjaxResult.success(fuwTaskService.selectFuwTaskByTaskId(taskId));
    }

    /**
     * 新增任务分配
     */
    @PreAuthorize("@ss.hasPermi('fuw:task:add')")
    @Log(title = "任务分配", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestParam("taskId") String taskId,
                          @RequestParam("taskName") String taskName,
                          @RequestParam("taskStartTime") Date taskStartTime,
                          @RequestParam("taskEndTime") Date taskEndTime,
                          @RequestParam("taskWeek") Date taskWeek,
                          @RequestParam("taskType") String taskType,
                          Integer taskNum,
                          @RequestParam("taskOutTime") Date taskOutTime,
                          @RequestParam("medicalId") String medicalId,
                          @RequestParam("workId") String workId)
    {
        // 自定义格式UI
        String uuidStr = "Task" + UUID.fastUUID();

        // 新增信息填入
        FuwTask fuwTask = new FuwTask();
        fuwTask.setTaskId(uuidStr);
        fuwTask.setTaskName(taskName);
        fuwTask.setTaskWeek(taskWeek);
        fuwTask.setTaskStartTime(taskStartTime);
        fuwTask.setTaskEndTime(taskEndTime);
        fuwTask.setTaskType("0");
        if(taskNum == null){
            fuwTask.setTaskNum(1);
        }else{
            fuwTask.setTaskNum(taskNum);
        }
        fuwTask.setMedicalId(medicalId);
        fuwTask.setWorkId(workId);

        /**
         * 新增记录
         */
        // 新增信息填入
        FuwTasks fuwTasks = new FuwTasks();
        fuwTasks.setTasksId(uuidStr);
        fuwTasks.setTasksName(taskName);
        fuwTasks.setTasksWeek(taskWeek);
        fuwTasks.setTasksStartTime(taskStartTime);
        fuwTasks.setTasksEndTime(taskEndTime);
        fuwTasks.setTasksType("0");
        if(taskNum == null){
            fuwTasks.setTasksNum(1);
        }else{
            fuwTasks.setTasksNum(taskNum);
        }
        fuwTasks.setMedicalId(medicalId);
        fuwTasks.setWorkId(workId);
        fuwTasksService.insertFuwTasks(fuwTasks);

        return toAjax(fuwTaskService.insertFuwTask(fuwTask));
    }

    /**
     * 修改任务分配
     */
    @PreAuthorize("@ss.hasPermi('fuw:task:edit')")
    @Log(title = "任务分配", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update")
    public AjaxResult edit(@RequestParam("taskId") String taskId,
                           @RequestParam("taskName") String taskName,
                           @RequestParam("taskStartTime") Date taskStartTime,
                           @RequestParam("taskEndTime") Date taskEndTime,
                           @RequestParam("taskWeek") Date taskWeek,
                           @RequestParam("taskType") String taskType,
                           Integer taskNum,
                           @RequestParam("taskOutTime") Date taskOutTime,
                           @RequestParam("medicalId") String medicalId,
                           @RequestParam("workId") String workId)
    {
        // 查询任务记录
        FuwTask fuwTask = fuwTaskService.selectFuwTaskByTaskId(taskId);
        fuwTask.setTaskName(taskName);
        fuwTask.setTaskWeek(taskWeek);
        fuwTask.setTaskStartTime(taskStartTime);
        fuwTask.setTaskEndTime(taskEndTime);
        // 修改任务状态
        fuwTask.setTaskType(taskType);

        if(taskNum == null){
            fuwTask.setTaskNum(1);
        }else{
            fuwTask.setTaskNum(taskNum);
        }
        fuwTask.setTaskOutTime(taskOutTime);
        fuwTask.setMedicalId(medicalId);
        fuwTask.setWorkId(workId);

        return toAjax(fuwTaskService.updateFuwTask(fuwTask));
    }

    /**
     * 修改任务分配
     */
    @PreAuthorize("@ss.hasPermi('fuw:task:edit')")
    @Log(title = "任务分配", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update/type")
    public AjaxResult editType(@RequestParam("taskId") String taskId,
                           @RequestParam("taskType") String taskType)
    {
        // 查询任务记录
        FuwTask fuwTask = fuwTaskService.selectFuwTaskByTaskId(taskId);
        // 修改任务状态
        fuwTask.setTaskType(taskType);
        fuwTask.setTaskOutTime(DateUtils.parseDate(DateUtils.getTime()));

        // 修改任务记录状态
        FuwTasks fuwTasks = fuwTasksService.selectFuwTasksByTasksId(taskId);
        fuwTasks.setTasksType(taskType);
        fuwTasks.setTasksOutTime(DateUtils.parseDate(DateUtils.getTime()));
        fuwTasksService.updateFuwTasks(fuwTasks);

        return toAjax(fuwTaskService.updateFuwTask(fuwTask));
    }

    /**
     * 删除任务分配
     */
    @PreAuthorize("@ss.hasPermi('fuw:task:remove')")
    @Log(title = "任务分配", businessType = BusinessType.DELETE)
	@DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable String[] taskIds)
    {
        return toAjax(fuwTaskService.deleteFuwTaskByTaskIds(taskIds));
    }
}
